package cm.tidudada.config;


import cm.tidudada.entity.DataSourceEntity;
import com.alibaba.druid.pool.DruidDataSource;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.transaction.PlatformTransactionManager;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Consumer;

@Configuration
public class DynamicDataSourceConfig {
    private final Map<Object, Object> dataSourceMap = new HashMap<Object, Object>();

    @SuppressWarnings("all")
    @Autowired
    private DataSourceEntity dataSourceEntity;


    @SneakyThrows
    @PostConstruct
    public void init() {
        DriverManagerDataSource dds = new DriverManagerDataSource();
        dds.setDriverClassName(dataSourceEntity.getDriverClassName());
        dds.setPassword(dataSourceEntity.getPassword());
        dds.setUsername(dataSourceEntity.getUsername());
        dds.setUrl(dataSourceEntity.getUrl());
        List<Map<String, Object>> mapList = new JdbcTemplate(dds).queryForList("select * from sys_db_conf where delete_flag = 0");
        // 当值不是null是调用
        Optional.ofNullable(mapList).ifPresent(new Consumer<List<Map<String, Object>>>() {
            @Override
            public void accept(List<Map<String, Object>> maps) {
                maps.forEach(s->{
                    DruidDataSource druidDataSource = new DruidDataSource();
                    druidDataSource.setPassword(s.get("")+"");
                    druidDataSource.setUsername(s.get("")+"");
                    druidDataSource.setDriverClassName(s.get("")+"");
                    druidDataSource.setUrl(s.get("")+"");
                    dataSourceMap.put(s.get("")+"",druidDataSource);
                });
            }
        });
    }

    @Bean
    public DynamicDataSource dynamicDataSource() {
        DruidDataSource druid = new DruidDataSource();
        DynamicDataSource dynamicDataSource = new DynamicDataSource();
        druid.setUrl(dataSourceEntity.getUrl());
        druid.setUsername(dataSourceEntity.getUsername());
        druid.setPassword(dataSourceEntity.getPassword());
        druid.setDriverClassName(dataSourceEntity.getDriverClassName());
        dynamicDataSource.setDefaultTargetDataSource(druid);
        // 放入默认的数据源
        this.dataSourceMap.put("0",druid);
        dynamicDataSource.setTargetDataSources(dataSourceMap);
        return dynamicDataSource;
    }

    // 这个应该是进行数据源修改或更新时候调用的
    public void reshipment(){
        init();
        dynamicDataSource().setTargetDataSources(dataSourceMap);
    }

    @Bean
    public PlatformTransactionManager manager(){
        return new DataSourceTransactionManager(dynamicDataSource());
    }


}
